<?php

namespace App\Http\Validate\Cruise;

use App\Http\Validate\BaseValidate;
use App\Models\Cruise\CruiseTemplateModel;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;

class CruiseTemplateValidate extends BaseValidate
{
    public static function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    public static function getRules(array $fields = []): array
    {
        $params = request()->all();

        return self::getActuallyRules([
            'template.name'                                => 'required|string',
            'template.desc'                                => 'required|string',
            'template.type'                                => [
                'required',
                'integer',
                Rule::in(array_keys(CruiseTemplateModel::TYPE_TEXT))
            ],
            'template.sysTemplate'                         => [
                'required',
                'integer',
                Rule::in(array_keys(CruiseTemplateModel::SYS_TEMPLATE_TEXT))
            ],
            'template.antiCheatingSwitch'                  => Rule::when(
                in_array(
                    $params["template"]["type"] ?? null,
                    [CruiseTemplateModel::TYPE_3, CruiseTemplateModel::TYPE_8]
                ),
                "required|in:0,1",
                ""
            ),
            'template.status'                              => 'required|integer|between:0,1',
            'positions'                                    => 'required|array',
            'positions.*.positionId'                       => 'integer',
            'positions.*.positionName'                     => 'required|string',
            'positions.*.project'                          => 'required|array',
            'positions.*.project.*.projectId'              => 'required|integer',
            'positions.*.project.*.relations'              => 'array',
            'positions.*.project.*.relations.projectId'    => 'integer',
            'positions.*.project.*.relations.optionsValue' => 'array',
        ], $fields);
    }


    /**
     * 更新校验
     *
     * @param array $requestParams
     * @return array
     * @throws ValidationException
     */
    public static function addValidate(array $requestParams): array
    {
        return self::baseValidate($requestParams, array_keys(self::getRules()));
    }
}
